package cn.zh.npro.Fragment;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.zh.npro.Activity.Company;
import cn.zh.npro.Activity.Login;
import cn.zh.npro.Activity.MyCount;
import cn.zh.npro.Activity.Safy;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.DataCleanManager;

/**
 * Created by WYN on 2018/2/26 0026.
 */

public class MineFragment extends Fragment implements View.OnClickListener {

    LinearLayout mycount,cgcount,bkcount,cpinfo,
            ctsafy,myinfo,cache,itecode,about,share;
    Dialog dialog;
    int type=1;
    DataCleanManager dmanager;
    String str;
    TextView catxt;
    MyApplication mapp;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.mine_layout,container,false);
        mapp= (MyApplication) getActivity().getApplication();
        inimf(view);
        dmanager=new DataCleanManager();
        try {
            str = dmanager.getTotalCacheSize(getActivity());
            catxt.setText(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
        }

      public void inimf(View view){
          mycount=view.findViewById(R.id.mycount);
          cgcount=view.findViewById(R.id.cgcount);
          bkcount=view.findViewById(R.id.bkcount);
          cpinfo=view.findViewById(R.id.cpinfo);
          catxt=view.findViewById(R.id.catxt);
          ctsafy=view.findViewById(R.id.ctsafy);
          myinfo=view.findViewById(R.id.myinfo);
          cache=view.findViewById(R.id.cache);
          itecode=view.findViewById(R.id.itecode);
//          about=view.findViewById(R.id.about);
          share=view.findViewById(R.id.share);
          mycount.setOnClickListener(this);
          cgcount.setOnClickListener(this);
          bkcount.setOnClickListener(this);
          cpinfo.setOnClickListener(this);
          ctsafy.setOnClickListener(this);
          myinfo.setOnClickListener(this);
          cache.setOnClickListener(this);
          itecode.setOnClickListener(this);
//          about.setOnClickListener(this);
          share.setOnClickListener(this);
         }


    @Override
    public void onClick(View view) {
        int id=view.getId();
        Intent intent=null;
        switch (id){
            case R.id.mycount:
                intent=new Intent(getActivity(),MyCount.class);
                getActivity().startActivity(intent);
                break;
            case R.id.cgcount:
                type=1;
                getDialog();
                break;
            case R.id.bkcount:
                intent=new Intent(getActivity(),MyCount.class);
                getActivity().startActivity(intent);
                break;
            case R.id.cpinfo:
                intent=new Intent(getActivity(),Company.class);
                getActivity().startActivity(intent);
                break;
            case R.id.ctsafy:
                intent=new Intent(getActivity(),Safy.class);
                getActivity().startActivity(intent);
                break;
            case R.id.myinfo:

                break;
            case R.id.cache:
                type=2;
                getCache();
                break;
//            case R.id.about:
//
//                break;
            case R.id.share:

                break;
            case R.id.osure:
                 if (type==1){
                     SharedPreferences.Editor editor=mapp.getSp().edit();
                     editor.clear();
                     editor.commit();
                     Intent intent1=new Intent(getActivity(),Login.class);
                     startActivity(intent1);
                     getActivity().finish();
                 }else {
                     dmanager.clearAllCache(getActivity());
                     catxt.setText("0KB");
                 }
                dialog.dismiss();
                break;
            case R.id.ocancle:
                dialog.dismiss();
                break;
             }
          }


    public void getDialog(){
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View v = inflater.inflate(R.layout.out_layout, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.out_layout);// 加载布局
        v.findViewById(R.id.ocancle).setOnClickListener(this);
        v.findViewById(R.id.osure).setOnClickListener(this);
        dialog = new Dialog(getActivity(), R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("你确定要切换账号吗?");
        }

    public void getCache(){
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View v = inflater.inflate(R.layout.out_layout, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.out_layout);// 加载布局
        v.findViewById(R.id.ocancle).setOnClickListener(this);
        v.findViewById(R.id.osure).setOnClickListener(this);
        dialog = new Dialog(getActivity(), R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("你确定要清楚缓存吗?");
        }









    }
